//
//  GPLocationMessageCell.m
//  Keping
//
//  Created by 23 on 2017/12/7.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "GPLocationMessageCell.h"

@interface GPLocationMessageCell ()

/**背景*/
@property(nonatomic,weak) UIView *locationBgView;

/**地点名称*/
@property(nonatomic,weak) UILabel *locationNameLabel;
/**地点位置*/
@property(nonatomic,weak) UILabel *locationAddressLabel;
/**图片*/
@property(nonatomic,weak) UIImageView *locationView;


@end

@implementation GPLocationMessageCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        UIView *locationBgView = [[UIView alloc] init];
        locationBgView.backgroundColor = [UIColor whiteColor];
        [self.contentBackground addSubview:locationBgView];
        self.locationBgView = locationBgView;
        
        
        UILabel *locationNameLabel = [[UILabel alloc] init];
        locationNameLabel.text = @"地名";
        [self.contentBackground addSubview:locationNameLabel];
        self.locationNameLabel = locationNameLabel;
        
        UILabel *locationAddressLabel = [[UILabel alloc] init];
        locationAddressLabel.text = @"地址";
        locationAddressLabel.textColor = [UIColor darkGrayColor];
        locationAddressLabel.font = [UIFont systemFontOfSize:16.0];
        locationAddressLabel.numberOfLines = 0;
        [self.contentBackground addSubview:locationAddressLabel];
        self.locationAddressLabel = locationAddressLabel;
        
        UIImageView *locationView = [[UIImageView alloc] init];
        locationView.backgroundColor = [UIColor redColor];
        [self.contentBackground addSubview:locationView];
        self.locationView = locationView;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapContent)];
        [self.locationBgView addGestureRecognizer:tap];
        
        
        [locationNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.mas_equalTo(self.contentBackground).offset(10);
            make.right.mas_equalTo(self.contentBackground).offset(-10);
        }];
        
        [locationAddressLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.contentBackground).offset(10);
            make.top.mas_equalTo(self.locationNameLabel.mas_bottom).offset(10);
            make.right.mas_equalTo(self.contentBackground).offset(-10);
        }];
        
        [locationView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.bottom.mas_equalTo(self.contentBackground);
            make.top.mas_equalTo(self.locationAddressLabel.mas_bottom).offset(10);
            make.height.mas_equalTo(80);
        }];
        
        [locationBgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.top.bottom.mas_equalTo(self.contentBackground);
        }];
        
    }
    return self;
}


- (void)setMessage:(GPMessageItem *)message
{
    //设置父类的值和约束
    [super setMessage:message];
    
    [self.contentBackground mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(200);
    }];
    
    
    //取出数据
    self.locationAddressLabel.text = message.address;
    NSString *name = message.message.ext[@"name"];
    if (name == nil || name.length == 0) {
        name = message.address;
    }
    self.locationNameLabel.text = name;
    
    self.locationView.backgroundColor = [UIColor whiteColor];
    self.locationView.image = [UIImage imageNamed:@"map_placeholder"];
    
    //更新
    [self layoutIfNeeded];
    
    //计算cell的高度
    CGFloat cellHeight = CGRectGetMaxY(self.contentBackground.frame) + 10;
    message.cellHeight = cellHeight;
    
}

- (void)tapContent
{
    if ([self.delegate respondsToSelector:@selector(messageCell:didContentClicked:)]) {
        [self.delegate messageCell:self didContentClicked:self.message];
    }
}


@end
